package com.bruce.downloader.builder;

import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

/**
 * restTemplate构造器
 * <br>CreateDate  2021/09/15 16:23:04
 *
 * @author luotuan
 * @version 1.0.0
 */
public class RestTemplateBuilder {
    /**
     * 连接超时时间
     */
    private int connectTimeout = 50000;
    /**
     * 读取超时时间
     */
    private int readTimeout = 50000;

    public int getConnectTimeout() {
        return connectTimeout;
    }

    public RestTemplateBuilder setConnectTimeout(int connectTimeout) {
        this.connectTimeout = connectTimeout;
        return this;
    }

    public int getReadTimeout() {
        return readTimeout;
    }

    public RestTemplateBuilder setReadTimeout(int readTimeout) {
        this.readTimeout = readTimeout;
        return this;
    }

    public static RestTemplateBuilder builder() {
        return new RestTemplateBuilder();
    }

    public RestTemplate build() {
        final RestTemplate restTemplate = new RestTemplate();
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(this.connectTimeout);
        requestFactory.setReadTimeout(this.readTimeout);
        restTemplate.setRequestFactory(requestFactory);
        return restTemplate;
    }
}
